package com.zenithmind.disk.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.disk.pojo.domain.Share;
import com.zenithmind.disk.pojo.dto.ShareDTO;
import com.zenithmind.disk.pojo.query.ShareQuery;
import com.zenithmind.disk.pojo.vo.FileVO;
import com.zenithmind.disk.pojo.vo.ShareVO;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.disk.service.ShareService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 分享管理控制器
 * 遵循单一职责原则：仅负责HTTP请求映射和响应处理
 * 遵循依赖倒置原则：依赖抽象接口而非具体实现
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/share")
@Tag(name = "分享管理")
@RequiredArgsConstructor
@Validated
public class ShareController {

    private final ShareService shareService;

    @Log(description = "创建分享", operationType = OperationTypeEnum.ADD)
    @Operation(summary = "创建分享")
    @PostMapping("/createShare")
    public Result<ShareVO> createShare(
            @Validated @RequestBody ShareDTO shareDTO,
            @RequestParam("userId") String userId) {
        log.info("创建分享请求：分享信息={}, 用户ID={}", shareDTO, userId);
        ShareVO share = shareService.createShare(shareDTO, userId);
        return Result.success(share);
    }

    @Log(description = "获取公开分享", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "获取公开分享")
    @GetMapping("/getPublicShare")
    public Result<FileVO> getPublicShare(
            @RequestParam("shareCode") String shareCode,
            @RequestParam(value = "password", required = false) String password) {
        log.info("获取公开分享请求：分享码={}, 密码={}", shareCode, password != null ? "***" : "无");
        FileVO share = shareService.getPublicShare(shareCode, password);
        return Result.success(share);
    }

    @Log(description = "获取我的分享", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "获取我的分享")
    @GetMapping("/listMyShares")
    public Result<List<ShareVO>> listMyShares(@RequestParam("userId") String userId) {
        log.info("获取我的分享请求：用户ID={}", userId);
        List<ShareVO> shares = shareService.listMyShares(userId);
        return Result.success(shares);
    }

    @Log(description = "取消分享", operationType = OperationTypeEnum.UPDATE)
    @Operation(summary = "取消分享")
    @PutMapping("/cancelShare")
    public Result<Boolean> cancelShare(
            @RequestParam("shareId") String shareId,
            @RequestParam("userId") String userId) {
        log.info("取消分享请求：分享ID={}, 用户ID={}", shareId, userId);
        boolean result = shareService.cancelShare(shareId, userId);
        return Result.success(result);
    }

    @Log(description = "验证分享密码", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "验证分享密码")
    @GetMapping("/validateSharePassword")
    public Result<Boolean> validateSharePassword(@RequestParam("shareCode") String shareCode, @RequestParam(value = "password", required = false) String password) {
        boolean cancel = shareService.validateSharePassword(shareCode, password);
        return Result.success(cancel);
    }

    @Log(description = "下载分享文件", operationType = OperationTypeEnum.DOWNLOAD)
    @Operation(summary = "下载分享文件")
    @GetMapping("/downloadShare")
    public void downloadShare(@RequestParam("shareCode") String shareCode, @RequestParam(value = "password", required = false) String password, HttpServletResponse response) throws IOException {
        try {
            shareService.downloadShare(shareCode, password, response);
        } catch (Exception e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
        }
    }

    @Log(description = "下载分享文件夹中的指定文件", operationType = OperationTypeEnum.DOWNLOAD)
    @Operation(summary = "下载分享文件夹中的指定文件")
    @GetMapping("/downloadSharedFile")
    public void downloadSharedFile(
            @RequestParam("shareCode") String shareCode,
            @RequestParam("fileId") String fileId,
            @RequestParam(value = "password", required = false) String password,
            HttpServletResponse response) throws IOException {
        try {
            shareService.downloadSharedFile(shareCode, fileId, password, response);
        } catch (Exception e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
        }
    }

    @Log(description = "分页查询分享列表", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "分页查询分享列表")
    @GetMapping("/sharePage")
    public Result<PageResult<ShareVO>> sharePage(ShareQuery shareQuery) {
        log.info("分页查询分享列表：{}", shareQuery);
        PageResult<ShareVO> shareVOPageResult = shareService.sharePage(shareQuery);
        return Result.success(shareVOPageResult);
    }

    @Log(description = "批量删除分享", operationType = OperationTypeEnum.DELETE)
    @Operation(summary = "批量删除分享-管理员")
    @DeleteMapping("/deleteBatchShares")
    public Result<Boolean> deleteBatchShares(@RequestBody List<String> ids) {
        log.info("批量删除分享：{}", ids);
        boolean result = shareService.deleteBatchShares(ids);
        return Result.success(result);
    }

    @Log(description = "添加或修改分享", operationType = OperationTypeEnum.UPDATE)
    @Operation(summary = "添加或修改分享-管理员")
    @PostMapping("/addOrUpdateShare")
    public Result<ShareVO> addOrUpdateShare(@RequestBody Share share) {
        log.info("添加或修改分享：{}", share);
        ShareVO shareVO = shareService.addOrUpdateShareByAdmin(share);
        return Result.success(shareVO);
    }
}
